<template>
<view class="content">
	<view :style='{"width":"100%","padding":"0","position":"relative","background":"#fff","height":"100%"}'>
		<form :style='{"width":"calc(100% - 40rpx)","padding":"0","margin":"32rpx 20rpx","background":"none","display":"block","height":"auto"}' class="app-update-pv">
			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">报修单号</view>
				<view :style='{"padding":"0px 24rpx","margin":"0px","lineHeight":"80rpx","fontSize":"28rpx","color":"#50605d","flex":"1"}' class="right-input">
					{{ruleForm.baoxiudanhao}}
				</view>
			</view>
			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">设备名称</view>
				<input :style='{"border":"0","padding":"0px 24rpx","margin":"0px","color":"#50605d","borderRadius":"8rpx","flex":"1","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx","height":"80rpx"}' :disabled="ro.shebeimingcheng" v-model="ruleForm.shebeimingcheng" placeholder="设备名称"></input>
			</view>
			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">设备分类</view>
				<input :style='{"border":"0","padding":"0px 24rpx","margin":"0px","color":"#50605d","borderRadius":"8rpx","flex":"1","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx","height":"80rpx"}' :disabled="ro.shebeifenlei" v-model="ruleForm.shebeifenlei" placeholder="设备分类"></input>
			</view>
			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="" @tap="baoxiutupianTap">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">报修图片</view>
				<image :style='{"width":"80rpx","borderRadius":"100%","objectFit":"cover","display":"block","height":"80rpx"}' class="avator" v-if="ruleForm.baoxiutupian" :src="baseUrl+ruleForm.baoxiutupian.split(',')[0]" mode="aspectFill"></image>
				<image :style='{"width":"80rpx","borderRadius":"100%","objectFit":"cover","display":"block","height":"80rpx"}' class="avator" v-else src="../../static/gen/upload.png" mode="aspectFill"></image>
			</view>
			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">报修日期</view>
				<picker :style='{"width":"100%","flex":"1","height":"auto"}' mode="date" :value="ruleForm.baoxiuriqi" @change="baoxiuriqiChange">
					<view :style='{"width":"100%","lineHeight":"80rpx","fontSize":"28rpx","color":"#50605d"}' class="uni-input">{{ruleForm.baoxiuriqi?ruleForm.baoxiuriqi:"请选择报修日期"}}</view>
				</picker>
			</view>
			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">报修状态</view>
				<picker :style='{"width":"100%","flex":"1","height":"auto"}' @change="baoxiuzhuangtaiChange" :value="baoxiuzhuangtaiIndex" :disabled="true" :range="baoxiuzhuangtaiOptions">
					<view :style='{"width":"100%","lineHeight":"80rpx","fontSize":"28rpx","color":"#50605d"}' class="uni-input">{{ruleForm.baoxiuzhuangtai?ruleForm.baoxiuzhuangtai:"请选择报修状态"}}</view>
				</picker>
			</view>
			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">使用员</view>
				<input :style='{"border":"0","padding":"0px 24rpx","margin":"0px","color":"#50605d","borderRadius":"8rpx","flex":"1","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx","height":"80rpx"}' :disabled="ro.gonghao" v-model="ruleForm.gonghao" placeholder="使用员"></input>
			</view>
			
			<!-- 否 -->
 

			<view :style='{"padding":"0px 20rpx","margin":"0 0 24rpx 0","borderColor":"#c9f2e4","alignItems":"center","borderRadius":"40rpx","borderWidth":"2rpx","background":"#f3fcf9","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"width":"160rpx","padding":"0 20rpx 0 0","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","textAlign":"center】"}' class="title">报修内容</view>
				<textarea :style='{"border":"0","minHeight":"240rpx","padding":"24rpx","margin":"0px","color":"#50605d","borderRadius":"8rpx","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx"}' v-model="ruleForm.baoxiuneirong" placeholder="报修内容"></textarea>
			</view>
			
			
			<view :style='{"width":"100%","justifyContent":"space-between","display":"flex","height":"auto"}' class="btn" >
				<button :style='{"border":"0","padding":"0px","margin":"0","color":"rgb(255, 255, 255)","borderRadius":"40rpx","background":"#4F977E","width":"48%","lineHeight":"80rpx","fontSize":"28rpx","height":"80rpx"}' @tap="onSubmitTap" class="bg-red">提交</button>
			</view>
		</form>

	</view>
</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";
    import xiaEditor from '@/components/xia-editor/xia-editor';
    import multipleSelect from "@/components/momo-multipleSelect/momo-multipleSelect";
	export default {
		data() {
			return {
				cross:'',
				ruleForm: {
				baoxiudanhao: this.getUUID(),
				shebeimingcheng: '',
				shebeifenlei: '',
				baoxiutupian: '',
				baoxiuneirong: '',
				baoxiuriqi: '',
			        baoxiuzhuangtai: '未处理',
				gonghao: '',
				shhf: '',
				},
				baoxiuzhuangtaiOptions: [],
				baoxiuzhuangtaiIndex: 0,
				// 登录用户信息
				user: {},
                ro:{
                   baoxiudanhao : false,
                   shebeimingcheng : false,
                   shebeifenlei : false,
                   baoxiutupian : false,
                   baoxiuneirong : false,
                   baoxiuriqi : false,
                   baoxiuzhuangtai : false,
                   gonghao : false,
                   sfsh : false,
                   shhf : false,
                },
			}
		},
		components: {
			wPicker,
            xiaEditor,
            multipleSelect
		},
		computed: {
			baseUrl() {
				return this.$base.url;
			},



		},
		async onLoad(options) {
            this.ruleForm.baoxiuriqi = this.$utils.getCurDate();
			let table = uni.getStorageSync("nowTable");
			// 获取用户信息
			let res = await this.$api.session(table);
			this.user = res.data;
			
			// ss读取
			this.ruleForm.gonghao = this.user.gonghao
			this.ro.gonghao = true;

            this.ro.baoxiudanhao = true;
            this.ro.shebeimingcheng = true;
            this.ro.shebeifenlei = true;
            this.ro.baoxiuriqi = true;
            this.ro.baoxiuzhuangtai = true;
            this.ro.gonghao = true;

			// 自定义下拉框值
			this.baoxiuzhuangtaiOptions = "已处理,未处理".split(',')

			// 如果有登录，获取登录后保存的userid
			this.ruleForm.userid = uni.getStorageSync("userid")
			if (options.refid) {
				// 如果上一级页面传递了refid，获取改refid数据信息
				this.ruleForm.refid = options.refid;
				this.ruleForm.nickname = uni.getStorageSync("nickname");
			}
			// 如果是更新操作
			if (options.id) {
				this.ruleForm.id = options.id;
				// 获取信息
				res = await this.$api.info(`baoxiugongdan`, this.ruleForm.id);
				this.ruleForm = res.data;
			}
			// 跨表
			this.cross = options.cross;
			if(options.cross){
				var obj = uni.getStorageSync('crossObj');
				for (var o in obj){
					if(o=='baoxiudanhao'){
					this.ruleForm.baoxiudanhao = obj[o];
					this.ro.baoxiudanhao = true;
					continue;
					}
					if(o=='shebeimingcheng'){
					this.ruleForm.shebeimingcheng = obj[o];
					this.ro.shebeimingcheng = true;
					continue;
					}
					if(o=='shebeifenlei'){
					this.ruleForm.shebeifenlei = obj[o];
					this.ro.shebeifenlei = true;
					continue;
					}
					if(o=='baoxiutupian'){
					this.ruleForm.baoxiutupian = obj[o].split(",")[0];
					this.ro.baoxiutupian = true;
					continue;
					}
					if(o=='baoxiuneirong'){
					this.ruleForm.baoxiuneirong = obj[o];
					this.ro.baoxiuneirong = true;
					continue;
					}
					if(o=='baoxiuriqi'){
					this.ruleForm.baoxiuriqi = obj[o];
					this.ro.baoxiuriqi = true;
					continue;
					}
					if(o=='baoxiuzhuangtai'){
					this.ruleForm.baoxiuzhuangtai = obj[o];
					this.ro.baoxiuzhuangtai = true;
					continue;
					}
					if(o=='gonghao'){
					this.ruleForm.gonghao = obj[o];
					this.ro.gonghao = true;
					continue;
					}
				}
			}
			this.styleChange()
            this.$forceUpdate()
		},
		methods: {
			styleChange() {
				this.$nextTick(()=>{
					// document.querySelectorAll('.app-update-pv . .uni-input-input').forEach(el=>{
					//   el.style.backgroundColor = this.addUpdateForm.input.content.backgroundColor
					// })
				})
			},

			// 多级联动参数

			baoxiuriqiChange(e) {
				this.ruleForm.baoxiuriqi = e.target.value;
				this.$forceUpdate();
			},


			// 下拉变化
			baoxiuzhuangtaiChange(e) {
				this.baoxiuzhuangtaiIndex = e.target.value
				this.ruleForm.baoxiuzhuangtai = this.baoxiuzhuangtaiOptions[this.baoxiuzhuangtaiIndex]
			},

			baoxiutupianTap() {
				let _this = this;
				this.$api.upload(function(res) {
					_this.ruleForm.baoxiutupian = 'upload/' + res.file;
					_this.$forceUpdate();
					_this.$nextTick(()=>{
						_this.styleChange()
					})
				});
			},

			getUUID () {
				return new Date().getTime();
			},
			async onSubmitTap() {





















//跨表计算判断
				var obj;
				if((!this.ruleForm.gonghao)){
					this.$utils.msg(`使用员不能为空`);
					return
				}
				//更新跨表属性
			       var crossuserid;
			       var crossrefid;
			       var crossoptnum;
				if(this.cross){
                    uni.setStorageSync('crossCleanType',true);
					var statusColumnName = uni.getStorageSync('statusColumnName');
					var statusColumnValue = uni.getStorageSync('statusColumnValue');
					if(statusColumnName!='') {
                        if(!obj) {
						    obj = uni.getStorageSync('crossObj');
                        }
						if(!statusColumnName.startsWith("[")) {
							for (var o in obj){
								if(o==statusColumnName){
									obj[o] = statusColumnValue;
								}

							}
							var table = uni.getStorageSync('crossTable');
							await this.$api.update(`${table}`, obj);
						} else {
						       crossuserid=Number(uni.getStorageSync('userid'));
						       crossrefid=obj['id'];
						       crossoptnum=uni.getStorageSync('statusColumnName');
						       crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
						}
					}
				}
				if(crossrefid && crossuserid) {
					this.ruleForm.crossuserid=crossuserid;
					this.ruleForm.crossrefid=crossrefid;
					let params = {
						page: 1,
						limit:10,
						crossuserid:crossuserid,
						crossrefid:crossrefid,
					}
					let res = await this.$api.list(`baoxiugongdan`, params);
					if (res.data.total >= crossoptnum) {
						this.$utils.msg(uni.getStorageSync('tips'));
                        uni.removeStorageSync('crossCleanType');
						return false;
					} else {
                //跨表计算
						if(this.ruleForm.id){
							await this.$api.update(`baoxiugongdan`, this.ruleForm);
						}else{
							await this.$api.add(`baoxiugongdan`, this.ruleForm);
						}
						this.$utils.msgBack('提交成功');
					}
				} else {
                //跨表计算
					if(this.ruleForm.id){
						await this.$api.update(`baoxiugongdan`, this.ruleForm);
					}else{
						await this.$api.add(`baoxiugongdan`, this.ruleForm);
					}
					this.$utils.msgBack('提交成功');
				}
			},
			optionsChange(e) {
				this.index = e.target.value
			},
			bindDateChange(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			toggleTab(str) {
				this.$refs[str].show();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		min-height: calc(100vh - 44px);
		box-sizing: border-box;
	}
</style>
